Skip to main content

Coding6 - ⏰ Reminder API Features

MethodEndpointDescription
POST/api/remindersCreate a reminder for a task
GET/api/remindersGet all reminders for a user

✅ Step 1: Create Reminder Model

File: src/model/reminder.model.ts

import mongoose, { Schema, Document } from 'mongoose';

export interface IReminder extends Document {
taskId: mongoose.Types.ObjectId;
userId: mongoose.Types.ObjectId;
remindAt: Date;
note?: string;
}

const ReminderSchema: Schema<IReminder> = new Schema({
taskId: { type: mongoose.Schema.Types.ObjectId, ref: 'Task', required: true },
userId: { type: mongoose.Schema.Types.ObjectId, ref: 'User', required: true },
remindAt: { type: Date, required: true },
note: { type: String },
}, {
timestamps: true,
});

export default mongoose.model<IReminder>('Reminder', ReminderSchema);

🔐 taskId and userId are references — so we can later populate task/user data if needed.


✅ Step 2: Create Reminder Controller

File: src/controller/reminder.controller.ts

import { Request, Response } from 'express';
import Reminder from '../model/reminder.model';

// Create a new reminder
export const createReminder = async (req: Request, res: Response): Promise<Response> => {
try {
const newReminder = await Reminder.create(req.body);
return res.status(201).json(newReminder);
} catch (error) {
return res.status(400).json({ message: 'Failed to create reminder', error });
}
};

// Get all reminders for a user
export const getAllReminders = async (req: Request, res: Response): Promise<Response> => {
try {
const userId = req.query.userId; // In real apps, get this from auth middleware
const reminders = await Reminder.find({ userId });
return res.json(reminders);
} catch (error) {
return res.status(500).json({ message: 'Failed to fetch reminders', error });
}
};

✅ Step 3: Create Reminder Routes

File: src/routes/reminder.routes.ts

import { Router } from 'express';
import { createReminder, getAllReminders } from '../controller/reminder.controller';

const router = Router();

router.post('/', createReminder);
router.get('/', getAllReminders);

export default router;

✅ Step 4: Register Routes in app.ts

import reminderRoutes from './routes/reminder.routes';
app.use('/api/reminders', reminderRoutes);

✅ Final Result

Your Reminder API is now live!

MethodEndpointWhat it does
POST/api/remindersCreate a reminder for a task
GET/api/remindersGet all reminders for a user